//
//  TJAddbankController.m
//  Teleport
//
//  Created by xtjun on 2020/6/13.
//  Copyright © 2020 xtjun. All rights reserved.
//

#import "TJAddbankController.h"


@interface TJAddbankController ()


@property (weak, nonatomic) UITextField *bankNameTextField;
@property (weak, nonatomic) UITextField *branchNameTextField;
@property (weak, nonatomic) UITextField *bankNumberTextField;
@property (weak, nonatomic) UIButton *saveButton;


@end

@implementation TJAddbankController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationBar.title = @"添加银行卡".localizedString;
    
    self.view.backgroundColor = UIColor_Hex(0xf2f2f2);
    
    UILabel *nameLabel = [UILabel lableWithSuperView:self.view font:UIFont_Px(14) color:UIColor.blackColor title:@"姓名*".localizedString textAlignment:NSTextAlignmentLeft];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.top.offset(KNavBarHight);
        make.height.offset(40);
    }];
    
    QMUITextField *nameTextField = [[QMUITextField alloc] init];
    nameTextField.textInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    
    [nameTextField shearRoundedCornersWithRadiu:3];
    nameTextField.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:nameTextField];
    // nameTextField.placeholder = @"请输入您的姓名".localizedString;
    [nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel);
        make.right.offset(-16);
        make.top.equalTo(nameLabel.mas_bottom);
        make.height.offset(40);
    }];
    //不能输入
    nameTextField.text = TJUserInfo.readInfo.uname;
    nameTextField.userInteractionEnabled = NO;
    
    UILabel *bankNumberLabel = [UILabel lableWithSuperView:self.view font:UIFont_Px(14) color:UIColor.blackColor title:@"银行卡号*".localizedString textAlignment:NSTextAlignmentLeft];
    [bankNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.top.equalTo(nameTextField.mas_bottom);
        make.height.offset(40);
    }];
    
    QMUITextField *bankNumberTextField = [[QMUITextField alloc] init];
    [bankNumberTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    bankNumberTextField.textInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [bankNumberTextField shearRoundedCornersWithRadiu:3];
    bankNumberTextField.backgroundColor = UIColor.whiteColor;
    bankNumberTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:bankNumberTextField];
    bankNumberTextField.placeholder = @"请输入银行卡号".localizedString;
    [bankNumberTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel);
        make.right.offset(-16);
        make.top.equalTo(bankNumberLabel.mas_bottom);
        make.height.offset(40);
    }];
    
    UILabel *bankNameLabel = [UILabel lableWithSuperView:self.view font:UIFont_Px(14) color:UIColor.blackColor title:@"开户银行*".localizedString textAlignment:NSTextAlignmentLeft];
    [bankNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.top.equalTo(bankNumberTextField.mas_bottom);
        make.height.offset(40);
    }];
    
    QMUITextField *bankNameTextField = [[QMUITextField alloc] init];
    [bankNameTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    bankNameTextField.textInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [bankNameTextField shearRoundedCornersWithRadiu:3];
    bankNameTextField.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:bankNameTextField];
    bankNameTextField.placeholder = @"请输入开户银行".localizedString;
    [bankNameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel);
        make.right.offset(-16);
        make.top.equalTo(bankNameLabel.mas_bottom);
        make.height.offset(40);
    }];
    
    
    UILabel *branchNameLabel = [UILabel lableWithSuperView:self.view font:UIFont_Px(14) color:UIColor.blackColor title:@"开户支行*".localizedString textAlignment:NSTextAlignmentLeft];
    [branchNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.top.equalTo(bankNameTextField.mas_bottom);
        make.height.offset(40);
    }];
    
    QMUITextField *branchNameTextField = [[QMUITextField alloc] init];
    [branchNameTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    branchNameTextField.textInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [branchNameTextField shearRoundedCornersWithRadiu:3];
    branchNameTextField.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:branchNameTextField];
    branchNameTextField.placeholder = @"请输入开户支行".localizedString;
    [branchNameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel);
        make.right.offset(-16);
        make.top.equalTo(branchNameLabel.mas_bottom);
        make.height.offset(40);
    }];
    
    
    UIButton *saveButton = [UIButton buttonWithSuperView:self.view font:UIFont_Px(18) color:UIColor.whiteColor title:@"保存".localizedString];
    saveButton.backgroundColor = UIColor.tj_mainColor;
    [saveButton shearRoundedCornersWithRadiu:22];
    [saveButton addTarget:self action:@selector(didClickSaveButton) forControlEvents:UIControlEventTouchUpInside];
    [saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-34);
        make.left.offset(34);
        make.height.offset(44);
        make.centerX.offset(0);
        make.bottom.offset(-TabbarSafeBottomMargin - 64);
    }];
    nameTextField.font = UIFont_Px(15);
    branchNameTextField.font = UIFont_Px(15);
    bankNameTextField.font = UIFont_Px(15);
    bankNumberTextField.font = UIFont_Px(15);
    
    /**卡号 */
    _bankNumberTextField = bankNumberTextField;
    /**开户银行 */
    _bankNameTextField = bankNameTextField;
    /**支行 */
    _branchNameTextField = branchNameTextField;
    
    _saveButton = saveButton;
    
    TJUserInfo *userInfo = TJUserInfo.readInfo;
    if (userInfo.bankCard.replacingEmptyString.length) {//有值 修改
        
        [self editSetUp];
        //赋值
        self.bankNumberTextField.text = userInfo.bankCard;
        self.bankNameTextField.text = userInfo.bankName;
        self.branchNameTextField.text = userInfo.bankAddress;
        
    }
    
    
    
    //默认置灰
    [self textFieldDidChange: _bankNumberTextField];
    
}

- (void)didClickSaveButton {

    NSString *name = TJUserInfo.readInfo.uname;
    NSString *bankNumber = self.bankNumberTextField.text;
    NSString *bankName = self.bankNameTextField.text;
    NSString *branchName = self.branchNameTextField.text;
    
    [self showloading];
    
    NSDictionary *parameters = @{
        @"userId": TJUserInfo.readInfo.userId,
        @"bankUserName": name,
        @"bankCard": bankNumber,
        @"bankName": bankName,
        @"bankAddress": branchName,
    };
    
    [TJNetworking.shareNetworking POSTNew:@"member/member/setBankInfo" parameters:parameters success:^(NSDictionary * _Nonnull responseObject, NSArray *data) {
        
        [self hiddenloading];
        
        [self.navigationController popToRootViewControllerAnimated:YES];
        
    } failure:^(NSError * _Nonnull error) {
        [TJHud showAutoHideMessage:error.localizedFailureReason];
    }];
    
}

- (void)textFieldDidChange:(UITextField *)textField {
    _saveButton.userInteractionEnabled =  _branchNameTextField.text.replacingEmptyString.length && _bankNameTextField.text.replacingEmptyString.length && _bankNumberTextField.text.replacingEmptyString.length;
    _saveButton.backgroundColor = (_saveButton.userInteractionEnabled) ? UIColor.tj_mainColor : [UIColor_Hex(0x666666) colorWithAlphaComponent:0.3];
}
- (void)editSetUp {
    self.saveButton.hidden  = YES;
    self.navigationBar.rightBarButton = [TJButtonBarButton barButtonWithTitle:@"解绑".localizedString color:UIColor.tj_mainColor target:self action:@selector(didClickNavBarRightButton)];
    
}
- (void)didClickNavBarRightButton{
    
    TJUIAlertController *alertController = [TJUIAlertController alertControllerWithTitle:@"提示".localizedString message:@"是否解绑当前收付款方式?".localizedString preferredStyle:QMUIAlertControllerStyleAlert];
    
    
    [alertController addAction:[QMUIAlertAction actionWithTitle:@"确定解绑".localizedString style:QMUIAlertActionStyleDestructive handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        self.saveButton.hidden = NO;
        
    }]];
    
    [alertController addAction:[QMUIAlertAction actionWithTitle:@"再想想".localizedString style:QMUIAlertActionStyleCancel handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        
    }]];
    
    alertController.alertCancelButtonAttributes = @{NSForegroundColorAttributeName:UIColor_Hex(0x9EA0A4),NSFontAttributeName:UIFontMake(16), NSKernAttributeName:@(0)};
    alertController.alertDestructiveButtonAttributes = @{NSForegroundColorAttributeName:UIColorRed,NSFontAttributeName:UIFontMake(16),NSKernAttributeName:@(0)};
    [alertController showWithAnimated:YES];
    
}


@end


